Imports System.Reflection
Imports System.Collections.Generic
Imports System.Linq
Imports Microsoft.Xna.Framework
Imports Microsoft.Xna.Framework.Audio
Imports Microsoft.Xna.Framework.Content
Imports Microsoft.Xna.Framework.GamerServices
Imports Microsoft.Xna.Framework.Graphics
Imports Microsoft.Xna.Framework.Input
Imports Microsoft.Xna.Framework.Media
Imports Microsoft.Xna.Framework.Input.Touch
Imports System.IO




Public Class Player
	Inherits Sprite


	''' <summary>
	''' Constructors a new player.
	''' </summary>
	Public Sub New(room As RoomNew, position As Vector2, GraphicsDevice__1 As GraphicsDevice, spriteEffect As SpriteEffects)
		graphicsDevice = GraphicsDevice__1
		spriteRoom = room
		LoadContent()

		'TAKE PLAYER Position
		Reset(position, spriteEffect)
	End Sub




	''' <summary>
	''' Loads the player sprite sheet and sounds.
	''' </summary>
	''' <note>i will add a parameter read form app.config</note>
	''' 
	''' 
	Private Sub LoadContent()

		spriteSequence = New List(Of Sequence)()
		Dim ax As New System.Xml.Serialization.XmlSerializer(spriteSequence.[GetType]())

		Dim txtReader As Stream = Microsoft.Xna.Framework.TitleContainer.OpenStream(PrinceOfPersiaGame.CONFIG_PATH_CONTENT & PrinceOfPersiaGame.CONFIG_PATH_SEQUENCES & "KID_sequence.xml")


		'TextReader txtReader = File.OpenText(PrinceOfPersiaGame.CONFIG_PATH_CONTENT + PrinceOfPersiaGame.CONFIG_PATH_SEQUENCES + "KID_sequence.xml");
		'Stream astream = this.GetType().Assembly.GetManifestResourceStream("PrinceOfPersia.resources.KID_sequence.xml");
		spriteSequence = DirectCast(ax.Deserialize(txtReader), List(Of Sequence))

		For Each s As Sequence In spriteSequence
			s.Initialize(SpriteRoom.content)
		Next

		' Calculate bounds within texture size.         
		'faccio un rettangolo che sia largo la metà del frame e che parta dal centro
		Dim top As Integer = 0
		'StandAnimation.FrameHeight - height - 128;
		Dim left As Integer = 0
		'PLAYER_L_PENETRATION; //THE LEFT BORDER!!!! 19
		Dim width As Integer = 114
		'(int)(StandAnimation.FrameWidth);  //lo divido per trovare punto centrale *2)
		Dim height As Integer = 114
		'(int)(StandAnimation.FrameHeight);

		localBounds = New Rectangle(left, top, width, height)

		' Load sounds.            
		'killedSound = _room.content.Load<SoundEffect>("Sounds/PlayerKilled");
		'jumpSound = _room.content.Load<SoundEffect>("Sounds/PlayerJump");
		'fallSound = _room.content.Load<SoundEffect>("Sounds/PlayerFall");
	End Sub




	Public Sub Reset()
		Reset(startPosition, startFlip)
	End Sub

	''' <summary>
	''' Resets the player to life.
	''' </summary>
	''' <param name="position">The position to come to life at.</param>
	Public Sub Reset(position As Vector2, spriteEffect As SpriteEffects)
		startPosition = position

		_position = New Position(New Vector2(graphicsDevice.Viewport.Width, graphicsDevice.Viewport.Height), New Vector2(Player.SPRITE_SIZE_X, Player.SPRITE_SIZE_Y))
		_position.X = position.X
		_position.Y = position.Y

		Velocity = Vector2.Zero
		Energy = PrinceOfPersiaGame.CONFIG_KID_START_ENERGY

		flip = spriteEffect

		spriteState.Clear()


		Stand()

	End Sub

	'''// <summary>
	'''// Start position the player to life.
	'''// </summary>
	'''// <param name="position">The position to come to life at.</param>
    Public Sub Start(position As Vector2)
        _position.X = position.X
        _position.Y = position.Y
        Velocity = Vector2.Zero
        Energy = PrinceOfPersiaGame.CONFIG_KID_START_ENERGY


        spriteState.Clear()

        Stand()

    End Sub

	''' <summary>
	''' Handles input, performs physics, and animates the player sprite.
	''' </summary>
	''' <remarks>
	''' We pass in all of the input states so that our game is only polling the hardware
	''' once per frame. We also pass the game's orientation because when using the accelerometer,
	''' we need to reverse our motion when the orientation is in the LandscapeRight orientation.
	''' </remarks>
	Public Sub Update(gameTime As GameTime, keyboardState As KeyboardState, gamePadState As GamePadState, touchState As TouchCollection, accelState As AccelerometerState, orientation As DisplayOrientation)
		Dim input As Enumeration.Input = GetInput(keyboardState, gamePadState, touchState, accelState, orientation)
		ParseInput(input)

		If IsAlive = False And isOnGround = True Then
			DeadFall()
			Return
		End If

		'ApplyPhysicsNew(gameTime);
		HandleCollisionsNew()


		Dim elapsed As Single = CSng(gameTime.ElapsedGameTime.TotalSeconds)
		' TODO: Add your game logic here.
		sprite.UpdateFrame(elapsed, _position, flip, spriteState)
	End Sub

	Public Sub ParseInput(input As Enumeration.Input)
		'if (spriteState.Value().state == Enumeration.State.question)
		'    Question();


		If spriteState.Value().Priority = Enumeration.PriorityState.Normal And sprite.IsStoppable = False Then
			Return
		End If

		Select Case input
			Case Enumeration.Input.none
				Select Case spriteState.Value().state
					Case Enumeration.State.none
						Stand(spriteState.Value().Priority)
						Exit Select
					Case Enumeration.State.stand
						Stand(spriteState.Value().Priority)
						Exit Select
					Case Enumeration.State.startrun
						RunStop()
						Exit Select
					Case Enumeration.State.running
						RunStop()
						Exit Select
					Case Enumeration.State.step1
						Stand()
						Exit Select
					Case Enumeration.State.stepfall
						StepFall(spriteState.Value().Priority, spriteState.Value().OffSet)
						Exit Select
					Case Enumeration.State.crouch
						StandUp()
						Exit Select
					Case Enumeration.State.highjump
						Stand()
						Exit Select
					Case Enumeration.State.hangstraight, Enumeration.State.hang
						HangDrop()
						Exit Select
					Case Enumeration.State.bump
						Bump(spriteState.Value().Priority)
						Exit Select
					Case Else

						Exit Select
				End Select
				Exit Select
			'LEFT//////////////////////
			Case Enumeration.Input.left
				Select Case spriteState.Value().state
					Case Enumeration.State.stand
						If flip = SpriteEffects.FlipHorizontally Then
							Turn()
						Else
							StartRunning()
						End If
						Exit Select
					Case Enumeration.State.step1
						StartRunning()
						Exit Select
					Case Enumeration.State.crouch
						If flip = SpriteEffects.FlipHorizontally Then
							Return
						End If
						Crawl()
						Exit Select
					Case Enumeration.State.stepfall
						StepFall()
						Exit Select
					Case Enumeration.State.startrun
						If flip = SpriteEffects.FlipHorizontally Then
							RunTurn()
						End If
						Exit Select
					'case Enumeration.State.hang:
					'    HangDrop();
					'    break;
					Case Enumeration.State.bump
						Bump(spriteState.Value().Priority)
						Exit Select
					Case Enumeration.State.ready
						If flip = SpriteEffects.FlipHorizontally Then
							Retreat()
						Else
							Advance()
						End If
						Exit Select
					Case Else

						Exit Select
				End Select
				Exit Select
			'SHIFTLEFT//////////////////////
			Case Enumeration.Input.leftshift
				Select Case spriteState.Value().state
					Case Enumeration.State.stand
						If flip = SpriteEffects.FlipHorizontally Then
							Turn()
						Else
							StepForward()
						End If
						Exit Select
					Case Enumeration.State.hangstraight, Enumeration.State.hang
						Hang()
						Exit Select
					Case Enumeration.State.bump
						Bump(spriteState.Value().Priority)
						Exit Select
					Case Enumeration.State.ready
						Strike(spriteState.Value().Priority)
						Exit Select
					Case Else


						Exit Select
				End Select
				Exit Select
			'LEFTDOWN//////////////////////
			Case Enumeration.Input.leftdown
				Select Case spriteState.Value().state
					Case Enumeration.State.crouch
						If flip = SpriteEffects.None Then
							Crawl()
						End If
						Exit Select
					Case Enumeration.State.startrun
						Stoop(New Vector2(5, 0))
						Exit Select
					Case Else
						Exit Select
				End Select
				Exit Select
			'LEFTUP//////////////////////
			Case Enumeration.Input.leftup
				Select Case spriteState.Value().state
					Case Enumeration.State.stand
						StandJump()
						Exit Select
					Case Enumeration.State.startrun
						RunJump()
						Exit Select
					Case Enumeration.State.stepfall
						StepFall()
						Exit Select
					Case Else
						Exit Select
				End Select
				Exit Select

			'RIGHT//////////////////////
			Case Enumeration.Input.right
				Select Case spriteState.Value().state
					Case Enumeration.State.stand
						If flip = SpriteEffects.None Then
							Turn()
						Else
							StartRunning()
						End If
						Exit Select
					Case Enumeration.State.step1
						StartRunning()
						Exit Select
					Case Enumeration.State.crouch
						If flip = SpriteEffects.None Then
							Return
						End If
						Crawl()
						Exit Select
					Case Enumeration.State.stepfall
						StepFall()
						Exit Select
					Case Enumeration.State.startrun
						If flip = SpriteEffects.None Then
							RunTurn()
						End If
						Exit Select
					Case Enumeration.State.ready
						If flip = SpriteEffects.FlipHorizontally Then
							Advance()
						Else
							Retreat()
						End If
						Exit Select
					Case Else

						Exit Select
				End Select
				Exit Select
			'SHIFTRIGHT//////////////////////
			Case Enumeration.Input.rightshift
				Select Case spriteState.Value().state
					Case Enumeration.State.stand
						If flip = SpriteEffects.None Then
							Turn()
						Else
							StepForward()
						End If
						Exit Select
					Case Enumeration.State.hang
						Hang()
						Exit Select
					Case Enumeration.State.ready
						Strike(spriteState.Value().Priority)
						Exit Select
					Case Else

						Exit Select
				End Select
				Exit Select
			'RIGHTDOWN//////////////////////
			Case Enumeration.Input.righdown
				Select Case spriteState.Value().state
					Case Enumeration.State.crouch
						If flip <> SpriteEffects.None Then
							Crawl()
						End If
						Exit Select
					Case Enumeration.State.hang
						ClimbFail()
						Exit Select
					Case Enumeration.State.startrun
						Stoop(New Vector2(5, 0))
						Exit Select
					Case Else
						Exit Select
				End Select
				Exit Select
			'RIGHTUP//////////////////////
			Case Enumeration.Input.rightup
				Select Case spriteState.Value().state
					Case Enumeration.State.stand
						StandJump()
						Exit Select
					Case Enumeration.State.startrun
						RunJump()
						Exit Select
					Case Enumeration.State.stepfall
						StepFall()
						Exit Select
					Case Else
						Exit Select
				End Select
				Exit Select

			'DOWN//////////////////////
			Case Enumeration.Input.down
				Select Case spriteState.Value().state
					Case Enumeration.State.stand
						Stoop()
						Exit Select
					Case Enumeration.State.hang, Enumeration.State.hangstraight
						HangDrop()
						Exit Select
					Case Enumeration.State.startrun
						Stoop(New Vector2(5, 0))
						Exit Select
					Case Else

						Exit Select
				End Select
				Exit Select
			'UP//////////////////////
			Case Enumeration.Input.up
				Select Case spriteState.Value().state
					Case Enumeration.State.running, Enumeration.State.startrun
						RunStop()
						Exit Select
					Case Enumeration.State.stand
						HighJump()
						Exit Select
					Case Enumeration.State.jumphangLong, Enumeration.State.jumphangMed, Enumeration.State.hang, Enumeration.State.hangstraight
						ClimbUp()
						Exit Select
					Case Else

						Exit Select
				End Select
				Exit Select
			'SHIFT/////////////////////////
			Case Enumeration.Input.shift
				Select Case spriteState.Value().state
					'case Enumeration.State.hang:
					'    Hang();
					'    break;
					Case Enumeration.State.jumphangLong, Enumeration.State.jumphangMed
						Hang()
						Exit Select
					Case Enumeration.State.ready
						Strike(spriteState.Value().Priority)
						Exit Select
					Case Else
						CheckItemOnFloor()
						Exit Select
				End Select
				Exit Select
			Case Else

				Exit Select
		End Select


	End Sub


	''' <summary>
	''' Gets player horizontal movement and jump commands from input.
	''' </summary>
	Private Function GetInput(keyboardState As KeyboardState, gamePadState As GamePadState, touchState As TouchCollection, accelState As AccelerometerState, orientation As DisplayOrientation) As Enumeration.Input




		If spriteState.Value().Priority = Enumeration.PriorityState.Force Then
			Return Enumeration.Input.none
		End If



		
        If PrinceOfPersiaGame.CONFIG_DEBUG = True Then
            If keyboardState.IsKeyDown(Keys.NumPad8) Then
                SpriteRoom = Maze.UpRoom(SpriteRoom)
                Return Enumeration.Input.none
            End If
            If keyboardState.IsKeyDown(Keys.NumPad2) Then
                SpriteRoom = Maze.DownRoom(SpriteRoom)
                Return Enumeration.Input.none
            End If
            If keyboardState.IsKeyDown(Keys.NumPad4) Then
                SpriteRoom = Maze.LeftRoom(SpriteRoom)
                Return Enumeration.Input.none
            End If
            If keyboardState.IsKeyDown(Keys.NumPad6) Then
                SpriteRoom = Maze.RightRoom(SpriteRoom)
                Return Enumeration.Input.none
            End If
            If keyboardState.IsKeyDown(Keys.NumPad0) Then
                Maze.StartRoom().StartNewLife(graphicsDevice)
                Return Enumeration.Input.none
            End If
            If keyboardState.IsKeyDown(Keys.OemMinus) Then
                AnimationSequence.frameRate = AnimationSequence.frameRate - 0.1F
                Return Enumeration.Input.none
            End If
            If keyboardState.IsKeyDown(Keys.OemPlus) Then
                AnimationSequence.frameRate = AnimationSequence.frameRate + 0.1F
                Return Enumeration.Input.none
            End If
            If keyboardState.IsKeyDown(Keys.NumPad5) Then
                'Maze.player.Resheathe();
                Maze.player.Sword = True
                Maze.player.LivePoints = 15
                Maze.player.Energy = 15
                Return Enumeration.Input.none
            End If
        End If


		'''///////
		'SHIFT
		'''///////
		If keyboardState.GetPressedKeys().Count() = 1 AndAlso keyboardState.GetPressedKeys()(0) = (Keys.LeftShift Or Keys.RightShift) Then
			Return Enumeration.Input.shift
		End If


		'''///////
		'LEFT 
		'''///////
		If keyboardState.IsKeyDown(Keys.Up) And keyboardState.IsKeyDown(Keys.Left) Then
			Return Enumeration.Input.leftup
		ElseIf keyboardState.IsKeyDown(Keys.Down) And keyboardState.IsKeyDown(Keys.Left) Then
			Return Enumeration.Input.leftdown
		ElseIf keyboardState.IsKeyDown(Keys.Left) And (keyboardState.IsKeyDown(Keys.LeftShift) Or (keyboardState.IsKeyDown(Keys.RightShift))) Then
			Return Enumeration.Input.leftshift
		ElseIf gamePadState.IsButtonDown(Buttons.DPadLeft) OrElse keyboardState.IsKeyDown(Keys.Left) OrElse keyboardState.IsKeyDown(Keys.A) Then
			Return Enumeration.Input.left

		'''///////
		'RIGHT 
		'''///////
		ElseIf keyboardState.IsKeyDown(Keys.Up) And keyboardState.IsKeyDown(Keys.Right) Then
			Return Enumeration.Input.rightup
		ElseIf keyboardState.IsKeyDown(Keys.Down) And keyboardState.IsKeyDown(Keys.Right) Then
			Return Enumeration.Input.righdown
		ElseIf keyboardState.IsKeyDown(Keys.Right) And (keyboardState.IsKeyDown(Keys.LeftShift) Or (keyboardState.IsKeyDown(Keys.RightShift))) Then
			Return Enumeration.Input.rightshift

		ElseIf gamePadState.IsButtonDown(Buttons.DPadRight) OrElse keyboardState.IsKeyDown(Keys.Right) OrElse keyboardState.IsKeyDown(Keys.D) Then
			Return Enumeration.Input.right
		'''///
		'UP//
		'''///
		ElseIf gamePadState.IsButtonDown(Buttons.DPadUp) OrElse keyboardState.IsKeyDown(Keys.Up) OrElse keyboardState.IsKeyDown(Keys.W) Then
			Return Enumeration.Input.up
		'''////
		'DOWN/
		'''////
		ElseIf gamePadState.IsButtonDown(Buttons.DPadDown) OrElse keyboardState.IsKeyDown(Keys.Down) OrElse keyboardState.IsKeyDown(Keys.S) Then
			Return Enumeration.Input.down
		Else
			'''/////
			'NONE//
			Return Enumeration.Input.none
		End If

	End Function


	'WARNING routine buggedddd
	Private Function isBehind(tileBounds As Rectangle, playerBounds As Rectangle) As Boolean
		If flip = SpriteEffects.FlipHorizontally Then
			'if (playerBounds.X > tileBounds.X - PLAYER_R_PENETRATION - Tile.PERSPECTIVE)
			If playerBounds.X > tileBounds.X Then
				Return True
			Else
				Return False
			End If
		Else
			If playerBounds.X < tileBounds.X Then
				Return True
			Else
				Return False

			End If
		End If

	End Function



	'''// <summary>
	'''// 
	'''// </summary>
	'''// <returns>
	'''// null is no wall
	'''// true for bump +
	'''// false fro bump -
	'''// </returns>
	'''// 
	'private bool? IsFrontOfBlockNew(bool isForHang)
	'{
	'    // Get the player's bounding rectangle and find neighboring tiles.
	'    Rectangle playerBounds = _position.Bounding;
	'    Vector2 v2 = _room.getCenterTile(playerBounds);

	'    int x = (int)v2.X;
	'    int y = (int)v2.Y;


	'    Rectangle tileBounds = _room.GetBounds(x, y);
	'    Vector2 depth = RectangleExtensions.GetIntersectionDepth(playerBounds, tileBounds);
	'    TileCollision tileCollision = _room.GetCollision(x, y);
	'    TileType tileType = _room.GetType(x, y);



	'    if (tileType != TileType.block)
	'        return null;

	'    if (isForHang == true)
	'        return true;

	'    if (flip == SpriteEffects.FlipHorizontally)
	'    {
	'        if (depth.X <= -18 & depth.X > (-Tile.PERSPECTIVE - PLAYER_R_PENETRATION)) //18*3 = 54 step forward....
	'            return true;
	'        else if (depth.X <= (-Tile.PERSPECTIVE - PLAYER_R_PENETRATION))
	'            return false;
	'        else
	'            return null;
	'    }
	'    else
	'    {
	'        //if (depth.X >= 18 & depth.X <= 27) //18*3 = 54 step forward....
	'        if (depth.X >= 18 & depth.X < (Tile.PERSPECTIVE + PLAYER_L_PENETRATION)) //18*3 = 54 step forward....
	'            return true;
	'        else if (depth.X >= (Tile.PERSPECTIVE + PLAYER_L_PENETRATION))
	'            return false;
	'        else
	'            return null;
	'    }
	'}


	Private Function IsDownOfBlock(isForClimbDown As Boolean) As System.Nullable(Of Boolean)
		' Get the player's bounding rectangle and find neighboring tiles.
		Dim playerBounds As Rectangle = _position.Bounding
		Dim v4 As Vector4 = SpriteRoom.getBoundTiles(playerBounds)

		Dim x As Integer, y As Integer = 0

		If flip = SpriteEffects.FlipHorizontally Then
			x = CInt(Math.Truncate(v4.Y)) - 1
			y = CInt(Math.Truncate(v4.Z)) + 1
		Else
			x = CInt(Math.Truncate(v4.X))
			y = CInt(Math.Truncate(v4.W)) + 1
		End If

		Dim tileBounds As Rectangle = SpriteRoom.GetBounds(x, y)
		Dim depth As Vector2 = RectangleExtensions.GetIntersectionDepth(playerBounds, tileBounds)
		Dim tileCollision As Enumeration.TileCollision = SpriteRoom.GetCollision(x, y)
		Dim tileType As Enumeration.TileType = SpriteRoom.[GetType](x, y)



		If tileType <> Enumeration.TileType.block Then
			Return False
		End If

		If isForClimbDown = True Then
			Return True
		End If

		If flip = SpriteEffects.FlipHorizontally Then
			If depth.X <= -18 And depth.X > (-Tile.PERSPECTIVE - PLAYER_R_PENETRATION) Then
				'18*3 = 54 step forward....
				Return True
			ElseIf depth.X <= (-Tile.PERSPECTIVE - PLAYER_R_PENETRATION) Then
				Return False
			Else
				Return Nothing
			End If
		Else
			'if (depth.X >= 18 & depth.X <= 27) //18*3 = 54 step forward....
			If depth.X >= 18 And depth.X < (Tile.PERSPECTIVE + PLAYER_L_PENETRATION) Then
				'18*3 = 54 step forward....
				Return True
			ElseIf depth.X >= (Tile.PERSPECTIVE + PLAYER_L_PENETRATION) Then
				Return False
			Else
				Return Nothing
			End If
		End If
	End Function

	Private Function IsFrontOfBlock(isForHang As Boolean) As System.Nullable(Of Boolean)
		' Get the player's bounding rectangle and find neighboring tiles.
		Dim playerBounds As Rectangle = _position.Bounding
		Dim v4 As Vector4 = SpriteRoom.getBoundTiles(playerBounds)

		Dim x As Integer, y As Integer = 0

		If flip = SpriteEffects.FlipHorizontally Then
			x = CInt(Math.Truncate(v4.Y))
			y = CInt(Math.Truncate(v4.Z))
		Else
			x = CInt(Math.Truncate(v4.X))
			y = CInt(Math.Truncate(v4.W))
		End If

		Dim tileBounds As Rectangle = SpriteRoom.GetBounds(x, y)
		Dim depth As Vector2 = RectangleExtensions.GetIntersectionDepth(playerBounds, tileBounds)
		Dim tileCollision As Enumeration.TileCollision = SpriteRoom.GetCollision(x, y)
		Dim tileType As Enumeration.TileType = SpriteRoom.[GetType](x, y)



		If tileType <> Enumeration.TileType.block Then
			Return Nothing
		End If

		If isForHang = True Then
			Return True
		End If

		If flip = SpriteEffects.FlipHorizontally Then
			If depth.X <= -18 And depth.X > (-Tile.PERSPECTIVE - PLAYER_R_PENETRATION) Then
				'18*3 = 54 step forward....
				Return True
			ElseIf depth.X <= (-Tile.PERSPECTIVE - PLAYER_R_PENETRATION) Then
				Return False
			Else
				Return Nothing
			End If
		Else
			'if (depth.X >= 18 & depth.X <= 27) //18*3 = 54 step forward....
			If depth.X >= 18 And depth.X < (Tile.PERSPECTIVE + PLAYER_L_PENETRATION) Then
				'18*3 = 54 step forward....
				Return True
			ElseIf depth.X >= (Tile.PERSPECTIVE + PLAYER_L_PENETRATION) Then
				Return False
			Else
				Return Nothing
			End If
		End If
	End Function


	'public void isGround()
	'{
	'    isOnGround = false;

	'    RoomNew room = null;
	'    Rectangle playerBounds = _position.Bounding;
	'    Vector2 v2 = SpriteRoom.getCenterTile(playerBounds);
	'    Rectangle tileBounds = SpriteRoom.GetBounds((int)v2.X, (int)v2.Y);

	'    //Check if kid outside Room 
	'    if (v2.X < 0)
	'        room = Maze.LeftRoom(SpriteRoom);
	'    else
	'        room = SpriteRoom;

	'    if (v2.Y > 2)
	'    {
	'        isOnGround = false;
	'    }
	'    else if (v2.Y < 0)
	'    {
	'        isOnGround = false;
	'    }
	'    else
	'    {
	'        if (room.GetCollision((int)v2.X, (int)v2.Y) != Enumeration.TileCollision.Passable)
	'        {
	'            if (playerBounds.Bottom >= tileBounds.Bottom)
	'                isOnGround = true;
	'        }
	'    }


	'    if (isOnGround == false)
	'    {
	'        if (sprite.sequence.raised == false)
	'        {
	'            if (spriteState.Value().state == Enumeration.State.runjump)
	'            {
	'                spriteState.Add(Enumeration.State.rjumpfall, Enumeration.PriorityState.Force);
	'                sprite.PlayAnimation(spriteSequence, spriteState.Value());
	'            }
	'            else
	'            {
	'                if (spriteState.Previous().state == Enumeration.State.runjump)
	'                    spriteState.Add(Enumeration.State.stepfall, Enumeration.PriorityState.Force, new Vector2(20,15));
	'                else
	'                    if (spriteState.Value().state != Enumeration.State.freefall)
	'                        spriteState.Add(Enumeration.State.stepfall, Enumeration.PriorityState.Force);
	'            }
	'            SpriteRoom.LooseShake();
	'            //and for upper room...
	'            SpriteRoom.maze.UpRoom(SpriteRoom).LooseShake();
	'        }
	'        return;
	'    }


	'    //IS ON GROUND!
	'    if (spriteState.Value().state == Enumeration.State.freefall)
	'    {
	'        //Align to tile x
	'        _position.Y = tileBounds.Bottom - _position._spriteRealSize.Y;
	'        //CHECK IF LOOSE ENERGY...
	'        int Rem = 0;
	'        Rem = (int) Math.Abs(Position.Y - PositionFall.Y) / Tile.REALHEIGHT;

	'        if (Rem > 0)
	'        {
	'            Energy = Energy - Rem;
	'        }
	'        spriteState.Add(Enumeration.State.crouch, Enumeration.PriorityState.Force, false);
	'    }

	'}

	Public Sub CheckItemOnFloor()
		'Rectangle playerBounds = ;
		Dim v2 As Vector2 = SpriteRoom.getCenterTile(_position.Bounding)

		Dim x As Integer = CInt(Math.Truncate(v2.X))
		Dim y As Integer = CInt(Math.Truncate(v2.Y))

		Dim t As Tile = SpriteRoom.GetTile(x, y)

		If t.item IsNot Nothing Then
			If t.item.[GetType]() Is GetType(Flask) Then
				DrinkPotion()
			End If
			If t.item.[GetType]() Is GetType(Sword) Then
				PickupSword()
			End If

			SpriteRoom.SubsTile(v2, Enumeration.TileType.floor)
		End If

	End Sub

	Public Sub HandleCollisionsNew()
		isGround()

		'Check opposite sprite like guards..
		Dim thereAreEnemy As Boolean = False
		For Each s As Sprite In SpriteRoom.SpritesInRoom()
			Select Case s.[GetType]().Name
				Case "Guard"
					If True Then
						If s.IsAlive = False Then
							Exit Select
						End If

						thereAreEnemy = True
						If s.Position.CheckOnRow(Position) Then

							'ENGARDE
							If s.Position.CheckOnRowDistance(Position) >= 0 And s.Position.CheckOnRowDistance(Position) <= 3 And Alert = False Then
								If Sword = True Then
									Engarde(Enumeration.PriorityState.Force, True)
									Alert = True
								Else
									If s.Position.CheckOnRowDistancePixel(Position) >= 0 And s.Position.CheckOnRowDistancePixel(Position) <= 70 And Alert = False Then
										Energy = 0
										Return
									End If
								End If
							End If

							'STRIKE/HIT
							If s.Position.CheckOnRowDistancePixel(Position) >= 0 And s.Position.CheckOnRowDistancePixel(Position) <= 70 And Alert = True Then


								'Change Flip player..
								If Position.X > s.Position.X Then
									flip = SpriteEffects.None
								Else
									flip = SpriteEffects.FlipHorizontally
								End If

								If spriteState.Value().Name = Enumeration.State.strike.ToString().ToUpper() Then
									If s.spriteState.Value().Name <> Enumeration.State.readyblock.ToString().ToUpper() Then
										'RESET STRIKE
										spriteState.Value().Name = String.Empty
										Dim g As GameTime = Nothing
										s.Splash(False, g)

										'Splash splash = new Splash(SpriteRoom, Position.Value, graphicsDevice, SpriteEffects.None, false);
										'Maze.sprites.Add(splash);

										s.Energy = s.Energy - 1
										s.StrikeRetreat()
									Else
										System.Console.WriteLine("G->" & Enumeration.State.readyblock.ToString().ToUpper())
									End If
								End If

								If s.Energy = 0 Then
									Fastheathe()

								End If
							End If
						Else
							Alert = False
						End If
						Exit Select
					End If
				Case Else
					Exit Select

			End Select
		Next
		If thereAreEnemy = False And Alert = True Then
			Alert = False
			Stand()
		End If

		Dim playerBounds As Rectangle = _position.Bounding
		'Find how many tiles are near on the left
		Dim v4 As Vector4 = SpriteRoom.getBoundTiles(playerBounds)

		' For each potentially colliding Tile, warning the for check only the player row ground..W
		For y As Integer = CInt(Math.Truncate(v4.Z)) To CInt(Math.Truncate(v4.W))
			For x As Integer = CInt(Math.Truncate(v4.X)) To CInt(Math.Truncate(v4.Y))
				Dim tileBounds As Rectangle = SpriteRoom.GetBounds(x, y)
				Dim depth As Vector2 = RectangleExtensions.GetIntersectionDepth(playerBounds, tileBounds)
				Dim tileCollision As Enumeration.TileCollision = SpriteRoom.GetCollision(x, y)
				Dim tileType As Enumeration.TileType = SpriteRoom.[GetType](x, y)

				Select Case tileType
					Case Enumeration.TileType.spikes
						If IsAlive = False Then
							DirectCast(SpriteRoom.GetTile(x, y), Spikes).Open()
							Return
						End If

						If depth.X < (-Tile.PERSPECTIVE - PLAYER_R_PENETRATION + Player.PLAYER_STAND_FRAME) Then
							DirectCast(SpriteRoom.GetTile(x, y), Spikes).Open()
						ElseIf depth.X > (Tile.PERSPECTIVE + PLAYER_L_PENETRATION - Player.PLAYER_STAND_FRAME) Then
							'45
							DirectCast(SpriteRoom.GetTile(x, y), Spikes).Open()
						End If

						If depth.Y >= 0 Then
							If SpriteRoom.GetTile(x, y).tileState.Value().state = Enumeration.StateTile.open Then
								If Me.spriteState.Value().state = Enumeration.State.stand And Me.spriteState.Previous().state = Enumeration.State.bump Then
									Impale(Enumeration.PriorityState.Normal)
								End If
							End If
							If SpriteRoom.GetTile(x, y).tileState.Value().state = Enumeration.StateTile.opened Then
								If Me.spriteState.Value().state = Enumeration.State.startrun Then
									Impale(Enumeration.PriorityState.Normal)
								End If
							End If
						End If
						'if (flip == SpriteEffects.FlipHorizontally)
						'{
						'    if (depth.X < 10 & depth.Y >= Player.SPRITE_SIZE_Y)
						'    {
						'        ((Spikes)SpriteRoom.GetTile(x, y)).Open();
						'        if (depth.Y >= Player.SPRITE_SIZE_Y & (this.spriteState.Value().state == Enumeration.State.crouch | this.spriteState.Value().state == Enumeration.State.startrun))
						'            Impale();
						'    }
						'}
						'else
						'{
						'    if (depth.X < -10 & depth.Y <= Player.SPRITE_SIZE_Y)
						'    {
						'        ((Spikes)SpriteRoom.GetTile(x, y)).Open();
						'        if (this.spriteState.Value().state == Enumeration.State.crouch)
						'            Impale();
						'        if (this.spriteState.Value().state == Enumeration.State.startrun)
						'            Impale();

						'    }
						'}

						Exit Select

					Case Enumeration.TileType.loose
						If depth.X < (-Tile.PERSPECTIVE - PLAYER_R_PENETRATION + Player.PLAYER_STAND_FRAME) Then
							DirectCast(SpriteRoom.GetTile(x, y), Loose).Press()
						ElseIf depth.X > (Tile.PERSPECTIVE + PLAYER_L_PENETRATION - Player.PLAYER_STAND_FRAME) Then
							'45
							DirectCast(SpriteRoom.GetTile(x, y), Loose).Press()
						Else
							isLoosable()
						End If
						'if (flip == SpriteEffects.FlipHorizontally)
						'{

						'    if (depth.X < (-Tile.PERSPECTIVE - PLAYER_R_PENETRATION + Player.PLAYER_STAND_FRAME))
						'        ((Loose)SpriteRoom.GetTile(x, y)).Press();
						'    else
						'        isLoosable();
						'}
						'else
						'{
						'    if (depth.X > (Tile.PERSPECTIVE + PLAYER_L_PENETRATION)) //45
						'        ((Loose)SpriteRoom.GetTile(x, y)).Press();
						'    else
						'        isLoosable();
						'}
						Exit Select

					Case Enumeration.TileType.pressplate
						'if (flip == SpriteEffects.FlipHorizontally)
						'{
						If depth.X < (-Tile.PERSPECTIVE - PLAYER_R_PENETRATION) Then
							DirectCast(SpriteRoom.GetTile(x, y), PressPlate).Press()
						ElseIf depth.X > (Tile.PERSPECTIVE + PLAYER_L_PENETRATION) Then
							'45
							DirectCast(SpriteRoom.GetTile(x, y), PressPlate).Press()
						End If
						'}
						'else
						'{
						'    if (depth.X > (Tile.PERSPECTIVE + PLAYER_L_PENETRATION)) //45
						'        ((PressPlate)SpriteRoom.GetTile(x, y)).Press();
						'    if (depth.X > (Tile.PERSPECTIVE + PLAYER_L_PENETRATION)) //45
						'         ((PressPlate)SpriteRoom.GetTile(x, y)).Press();
						'}
						Exit Select
					Case Enumeration.TileType.gate, Enumeration.TileType.block
						If tileType = Enumeration.TileType.gate Then
							If DirectCast(SpriteRoom.GetTile(x, y), Gate).State = Enumeration.StateTile.opened Then
								Exit Select
							End If
						End If
						'if player are raised then not collide..


						'if sx wall i will penetrate..for perspective design
						If flip = SpriteEffects.FlipHorizontally Then
							'only for x pixel 
							If depth.X < (-Tile.PERSPECTIVE - PLAYER_R_PENETRATION) Then
								If spriteState.Value().state <> Enumeration.State.freefall And spriteState.Value().state <> Enumeration.State.highjump And spriteState.Value().state <> Enumeration.State.hang And spriteState.Value().state <> Enumeration.State.hangstraight And spriteState.Value().state <> Enumeration.State.hangdrop And spriteState.Value().state <> Enumeration.State.hangfall And spriteState.Value().state <> Enumeration.State.jumphangMed And spriteState.Value().state <> Enumeration.State.jumphangLong And spriteState.Value().state <> Enumeration.State.climbup And spriteState.Value().state <> Enumeration.State.climbdown Then
									_position.Value = New Vector2(_position.X + (depth.X - (-Tile.PERSPECTIVE - PLAYER_R_PENETRATION)), _position.Y)
									If sprite.sequence.raised = False Then
										Bump(Enumeration.PriorityState.Force)
									Else
										RJumpFall(Enumeration.PriorityState.Force)
									End If
									Return
								End If
							Else
								If sprite.sequence.raised = True Then
									_position.Value = New Vector2(_position.X, _position.Y)
								Else
									_position.Value = New Vector2(_position.X, _position.Y)
								End If
							End If
						Else
							If depth.X > (Tile.PERSPECTIVE + PLAYER_L_PENETRATION) Then
								'45
								'if(sprite.sequence.raised == false)
								If spriteState.Value().state <> Enumeration.State.freefall And spriteState.Value().state <> Enumeration.State.highjump And spriteState.Value().state <> Enumeration.State.hang And spriteState.Value().state <> Enumeration.State.hangstraight And spriteState.Value().state <> Enumeration.State.hangdrop And spriteState.Value().state <> Enumeration.State.hangfall And spriteState.Value().state <> Enumeration.State.jumphangMed And spriteState.Value().state <> Enumeration.State.jumphangLong And spriteState.Value().state <> Enumeration.State.climbup And spriteState.Value().state <> Enumeration.State.climbdown Then

									_position.Value = New Vector2(_position.X + (depth.X - (Tile.PERSPECTIVE + PLAYER_L_PENETRATION)), _position.Y)
									Bump(Enumeration.PriorityState.Force)
									Return
								End If
							ElseIf sprite.sequence.raised = True Then
								_position.Value = New Vector2(_position.X, _position.Y)
							Else
								_position.Value = New Vector2(_position.X, _position.Y)
							End If
						End If
						playerBounds = BoundingRectangle
						Exit Select

					'default:
					'    _position.Value = new Vector2(_position.X, tileBounds.Bottom);
					'    playerBounds = BoundingRectangle;
					'    break;

				End Select
			Next
		Next
		'???
		'previousBottom = playerBounds.Bottom;
		'check if out room
		If _position.Y > RoomNew.BOTTOM_LIMIT + 10 Then
			Dim room As RoomNew = Maze.DownRoom(SpriteRoom)
			SpriteRoom = room
			_position.Y = RoomNew.TOP_LIMIT + 27
			' Y=77
			'For calculate height fall from damage points calculations..


            PositionFall = New Vector2(Position.X, (Game1.CONFIG_SCREEN_HEIGHT - RoomNew.BOTTOM_LIMIT - PositionFall.Y))
		ElseIf _position.X >= RoomNew.RIGHT_LIMIT Then
			Dim room As RoomNew = Maze.RightRoom(SpriteRoom)
			SpriteRoom = room
			_position.X = RoomNew.LEFT_LIMIT + 10
		ElseIf _position.X <= RoomNew.LEFT_LIMIT Then
			Dim room As RoomNew = Maze.LeftRoom(SpriteRoom)
			SpriteRoom = room
			_position.X = RoomNew.RIGHT_LIMIT - 10
		ElseIf _position.Y < RoomNew.TOP_LIMIT - 10 Then
			Dim room As RoomNew = Maze.UpRoom(SpriteRoom)
			SpriteRoom = room
				'Y=270
			_position.Y = RoomNew.BOTTOM_LIMIT - 24
		End If

	End Sub


	''' <summary>
	''' Called when this player reaches the level's exit.
	''' </summary>
	Public Sub OnReachedExit()
		'sprite.PlayAnimation(celebrateAnimation);
	End Sub


	'public bool isSnapToPlayerGrid()
	'{
	'    //FIX SNAP TO THE GRID
	'    if (_position.X % PLAYER_GRID != 0)
	'    {
	'        return false;
	'    }
	'    return true;
	'}

	'public void SnapToPlayerGrid()
	'{
	'    FIX SNAP TO THE GRID
	'    if (Position.X % PLAYER_GRID != 0 & spriteState.Value() == spriteState.State.stand)
	'    {
	'        Position = new Vector2((float)Math.Round(Position.X / PLAYER_GRID) * PLAYER_GRID + ((Position.X % PLAYER_GRID) * movement), position.Y);
	'    }
	'}


	''' <summary>
	''' Draws the animated player.
	''' </summary>
	Public Sub Draw(gameTime As GameTime, spriteBatch As SpriteBatch)
		'DRAW REAL COORDINATE
		'sprite.DrawNew(gameTime, spriteBatch, _position.Value, PositionArrive, flip, 0.5f);

		'DRAW SPRITE
		sprite.DrawSprite(gameTime, spriteBatch, _position.Value, flip, 0.5F)

	End Sub




	'public void DeadFall()
	'{
	'    spriteState.Add(Enumeration.State.deadfall, Enumeration.PriorityState.Force);
	'    sprite.PlayAnimation(spriteSequence, spriteState.Value());
	'}

	Public Sub Impale(priority As Enumeration.PriorityState)
		spriteState.Add(Enumeration.State.impale, priority)
		sprite.PlayAnimation(spriteSequence, spriteState.Value())
		Energy = 0
	End Sub

	Public Sub Impale()
		Impale(Enumeration.PriorityState.Normal)
	End Sub

	Public Sub Turn()
		If flip = SpriteEffects.FlipHorizontally Then
			flip = SpriteEffects.None
		Else
			flip = SpriteEffects.FlipHorizontally
		End If

		spriteState.Add(Enumeration.State.turn)
		sprite.PlayAnimation(spriteSequence, spriteState.Value())
		spriteState.Add(Enumeration.State.stand)

	End Sub

	Public Sub RunStop()
		RunStop(Enumeration.PriorityState.Normal)
	End Sub

	Public Sub RunStop(priority As Enumeration.PriorityState)
		spriteState.Add(Enumeration.State.runstop)
		sprite.PlayAnimation(spriteSequence, spriteState.Value())
		spriteState.Add(Enumeration.State.stand)
	End Sub

	Public Sub StartRunning()
		'TODO: i will check if there a wall and do a BUMP...
		Dim isFront As System.Nullable(Of Boolean) = IsFrontOfBlock(False)
		If isFront = True Then
			Bump(Enumeration.PriorityState.Normal, Enumeration.SequenceReverse.Reverse)
			Return
		ElseIf isFront = False Then
			Bump(Enumeration.PriorityState.Normal, Enumeration.SequenceReverse.Normal)
			Return
		End If

		spriteState.Add(Enumeration.State.startrun)
		sprite.PlayAnimation(spriteSequence, spriteState.Value())
	End Sub

	Public Sub StepForward()
		If sprite.IsStoppable = False Then
			Return
		End If

		'TODO: i will check if there a wall and do a BUMP...
		Dim isFront As System.Nullable(Of Boolean) = IsFrontOfBlock(False)
		If isFront = True Then
			Bump(Enumeration.PriorityState.Normal, Enumeration.SequenceReverse.Reverse)
			Return
		ElseIf isFront = False Then
			Bump(Enumeration.PriorityState.Normal, Enumeration.SequenceReverse.Normal)
			Return
		End If


		spriteState.Add(Enumeration.State.fullstep)
		sprite.PlayAnimation(spriteSequence, spriteState.Value())
		spriteState.Add(Enumeration.State.stand)
	End Sub


	Public Sub Engarde()
		Engarde(Enumeration.PriorityState.Normal, Nothing)
	End Sub

	Public Sub Engarde(stoppable As System.Nullable(Of Boolean))
		Engarde(Enumeration.PriorityState.Normal, stoppable)
	End Sub

	Public Sub Engarde(priority As Enumeration.PriorityState, stoppable As System.Nullable(Of Boolean))
		Engarde(priority, stoppable, Vector2.Zero)
	End Sub

	Public Sub Engarde(priority As Enumeration.PriorityState, stoppable As System.Nullable(Of Boolean), offset As Vector2)
		If priority = Enumeration.PriorityState.Normal And sprite.IsStoppable = False Then
			Return
		End If
		'TODO: ??? to be moved on calling routine??
		If spriteState.Value().state = Enumeration.State.ready Then
			Return
		End If
		'if (spriteState.Value().state == Enumeration.State.engarde)
		'{ return; }
		'if (spriteState.Value().state == Enumeration.State.advance)
		'{ return; }

		spriteState.Add(Enumeration.State.engarde, priority, stoppable, offset)
		sprite.PlayAnimation(spriteSequence, spriteState.Value())
	End Sub


	Public Sub Crouch()
		Crouch(Enumeration.PriorityState.Normal, Nothing)
	End Sub

	Public Sub Crouch(priority As Enumeration.PriorityState)
		Crouch(priority, Nothing)
	End Sub

	Public Sub Crouch(stoppable As System.Nullable(Of Boolean))
		Crouch(Enumeration.PriorityState.Normal, stoppable)
	End Sub

	Public Sub Crouch(priority As Enumeration.PriorityState, stoppable As System.Nullable(Of Boolean))
		Crouch(Enumeration.PriorityState.Normal, stoppable, Vector2.Zero)
	End Sub

	Public Sub Crouch(priority As Enumeration.PriorityState, stoppable As System.Nullable(Of Boolean), offset As Vector2)
		If priority = Enumeration.PriorityState.Normal And sprite.IsStoppable = False Then
			Return
		End If

		spriteState.Add(Enumeration.State.crouch, priority, stoppable, offset)
		sprite.PlayAnimation(spriteSequence, spriteState.Value())
	End Sub


	Public Sub Crawl()
		'if (spriteState.Previous().state == Enumeration.State.crawl)
		'    return;

		spriteState.Add(Enumeration.State.crawl)
		sprite.PlayAnimation(spriteSequence, spriteState.Value())
	End Sub

	''' Remnber: for example the top gate is x=3 y=1
	''' first row bottom = 2 the top row = 0..
	''' </summary>
	''' <returns>
	''' true = climbable floor
	''' false = no climb
	''' 
	''' </returns>
	Private Function isClimbableDown() As Boolean

		' Get the player's bounding rectangle and find neighboring tiles.
		Dim playerBounds As Rectangle = _position.Bounding
		Dim v2 As Vector2 = SpriteRoom.getCenterTile(playerBounds)

		Dim x As Integer = CInt(Math.Truncate(v2.X))
		Dim y As Integer = CInt(Math.Truncate(v2.Y))


		'Rectangle tileBounds = _room.GetBounds(x, y);
		'Vector2 depth = RectangleExtensions.GetIntersectionDepth(playerBounds, tileBounds);
		Dim tileCollision As Enumeration.TileCollision
		' = _room.GetCollision(x, y);
		'Enumeration.TileType tileType;
		If flip = SpriteEffects.FlipHorizontally Then
			'tileType = _room.GetType(x-1, y);
			tileCollision = SpriteRoom.GetCollision(x - 1, y)
		Else
			'tileType = _room.GetType(x+1, y);
			tileCollision = SpriteRoom.GetCollision(x + 1, y)
		End If


		'if (tileType != Enumeration.TileType.space)
		If tileCollision <> Enumeration.TileCollision.Passable Then
			Return False
		End If

		'check is platform or gate forward up
		Dim xOffSet As Integer = 0
		If flip = SpriteEffects.FlipHorizontally Then
			xOffSet = -Tile.REALWIDTH + (Tile.PERSPECTIVE * 2)
		Else
			xOffSet = -Tile.PERSPECTIVE
		End If

		Dim tileBounds As Rectangle = SpriteRoom.GetBounds(x, y + 1)
		_position.Value = New Vector2(tileBounds.Center.X + xOffSet, _position.Y)
		Return True

	End Function

	''' <summary>
	''' Remnber: for example the top gate is x=3 y=1
	''' first row bottom = 2 the top row = 0..
	''' </summary>
	''' <returns>
	''' true = climbable floor
	''' false = no climb
	''' 
	''' </returns>
	Private Function isClimbable() As Enumeration.State

		' Get the player's bounding rectangle and find neighboring tiles.
		Dim playerBounds As Rectangle = _position.Bounding
		Dim v2 As Vector2 = SpriteRoom.getCenterTile(playerBounds)

		Dim x As Integer = CInt(Math.Truncate(v2.X))
		Dim y As Integer = CInt(Math.Truncate(v2.Y))


		'Rectangle tileBounds = _room.GetBounds(x, y);
		'Vector2 depth = RectangleExtensions.GetIntersectionDepth(playerBounds, tileBounds);
		Dim tileCollision As Enumeration.TileCollision = SpriteRoom.GetCollision(x, y - 1)
		Dim tileType As Enumeration.TileType
		' = _room.GetType(x, y - 1);

		'if (tileType == Enumeration.TileType.floor | tileType == Enumeration.TileType.gate)
		If tileCollision = Enumeration.TileCollision.Platform Then
			'CHECK KID IS UNDER THE FLOOR CHECK NEXT TILE
			If flip = SpriteEffects.FlipHorizontally Then
				x = x - 1
			Else
				x = x + 1
			End If
			tileCollision = SpriteRoom.GetCollision(x, y - 1)
			'tileType = _room.GetType(x, y - 1);
			'if (tileType != Enumeration.TileType.space)
			If tileCollision <> Enumeration.TileCollision.Passable Then
				Return Enumeration.State.none
			End If
			x = CInt(Math.Truncate(v2.X))
			'THE FLOOR FOR CLIMB UP
			Dim t As Tile = SpriteRoom.GetTile(New Vector2(x, y))
			If t.Type = Enumeration.TileType.gate Then
				If t.tileState.Value().state <> Enumeration.StateTile.opened Then
					Return Enumeration.State.none
				End If

			End If
		'else if (tileType == Enumeration.TileType.space)
		ElseIf tileCollision = Enumeration.TileCollision.Passable Then
			If flip = SpriteEffects.FlipHorizontally Then
				x = x + 1
			Else
				x = x - 1
			End If
			tileCollision = SpriteRoom.GetCollision(x, y - 1)
			'tileType = _room.GetType(x, y - 1);
			'if (tileType != Enumeration.TileType.floor & tileType != Enumeration.TileType.gate)
			If tileCollision <> Enumeration.TileCollision.Platform Then
				Return Enumeration.State.none
			End If

			Dim t As Tile = SpriteRoom.GetTile(New Vector2(x, y - 1))
			If t.Type = Enumeration.TileType.gate Then
				If t.tileState.Value().state <> Enumeration.StateTile.opened Then
					Return Enumeration.State.none
				End If

			End If
		Else
			Return Enumeration.State.none
		End If

		'If is a door close isnt climbable


		'check is platform or gate forward up
		Dim xOffSet As Integer = 0
		If flip = SpriteEffects.FlipHorizontally Then
			xOffSet = -Tile.REALWIDTH + Tile.PERSPECTIVE
		End If

		Dim tileBounds As Rectangle
		'if under kid there's not a platform change the point to climbup..

		If flip = SpriteEffects.FlipHorizontally Then
			tileCollision = SpriteRoom.GetCollision(x - 1, y)
		Else
			tileCollision = SpriteRoom.GetCollision(x + 1, y)
		End If


		If tileCollision = Enumeration.TileCollision.Passable And flip <> SpriteEffects.FlipHorizontally Then
			tileBounds = SpriteRoom.GetBounds(x, y)
			xOffSet = -26
			_position.Value = New Vector2(tileBounds.Center.X + xOffSet, _position.Y)
			Return Enumeration.State.jumphangMed
		End If

		tileBounds = SpriteRoom.GetBounds(x, y - 1)
		_position.Value = New Vector2(tileBounds.Center.X + xOffSet, _position.Y)
		Return Enumeration.State.jumphangLong

	End Function

	Private Function isLoosable() As Boolean

		' Get the player's bounding rectangle and find neighboring tiles.
		Dim playerBounds As Rectangle = _position.Bounding
		Dim v2 As Vector2 = SpriteRoom.getCenterTile(playerBounds)

		Dim x As Integer = CInt(Math.Truncate(v2.X))
		Dim y As Integer = CInt(Math.Truncate(v2.Y))


		'Rectangle tileBounds = _room.GetBounds(x, y);
		'Vector2 depth = RectangleExtensions.GetIntersectionDepth(playerBounds, tileBounds);
		Dim tileCollision As Enumeration.TileCollision = SpriteRoom.GetCollision(x, y - 1)
		Dim tileType As Enumeration.TileType
		' = _room.GetType(x, y - 1);

		'if (tileType == Enumeration.TileType.floor | tileType == Enumeration.TileType.gate)
		If tileCollision = Enumeration.TileCollision.Platform Then
			'CHECK KID IS UNDER THE FLOOR CHECK NEXT TILE
			Dim t As Tile = SpriteRoom.GetTile(x, y - 1)
			If t.Type <> Enumeration.TileType.loose Then
				Return False
			End If


			DirectCast(t, Loose).Press()
		End If
		Return False
	End Function


	Public Sub JumpHangMed()
		If sprite.IsStoppable = False Then
			Return
		End If

		spriteState.Add(Enumeration.State.jumphangMed)
		sprite.PlayAnimation(spriteSequence, spriteState.Value())
	End Sub


	Public Sub JumpHangLong()
		If sprite.IsStoppable = False Then
			Return
		End If

		spriteState.Add(Enumeration.State.jumphangLong)
		sprite.PlayAnimation(spriteSequence, spriteState.Value())
	End Sub

	Public Sub ClimbUp()
		If sprite.IsStoppable = False Then
			Return
		End If

		spriteState.Add(Enumeration.State.climbup)
		sprite.PlayAnimation(spriteSequence, spriteState.Value())
	End Sub


	Public Sub ClimbDown()
		If sprite.IsStoppable = False Then
			Return
		End If

		Dim isDownOfBlock__1 As Boolean = False

		If IsDownOfBlock(True) = True Then
			isDownOfBlock__1 = True
		End If

		spriteState.Add(Enumeration.State.climbdown, isDownOfBlock__1)
		sprite.PlayAnimation(spriteSequence, spriteState.Value())
	End Sub


	Public Sub StandUp()
		StandUp(Enumeration.PriorityState.Normal)
	End Sub
	Public Sub StandUp(priority As Enumeration.PriorityState)
		spriteState.Add(Enumeration.State.standup, priority)
		sprite.PlayAnimation(spriteSequence, spriteState.Value())
		spriteState.Add(Enumeration.State.stand, priority)
	End Sub

	Public Sub Stoop()
		Stoop(Enumeration.PriorityState.Normal)
	End Sub
	Public Sub Stoop(offSet As Vector2)
		Stoop(Enumeration.PriorityState.Normal, offSet)
	End Sub
	Public Sub Stoop(priority As Enumeration.PriorityState)
		Stoop(Enumeration.PriorityState.Normal, Vector2.Zero)
	End Sub
	Public Sub Stoop(priority As Enumeration.PriorityState, offSet As Vector2)
		If isClimbableDown() = True Then
			ClimbDown()
			Return
		End If
		spriteState.Add(Enumeration.State.stoop, priority, Nothing, Enumeration.SequenceReverse.Normal, offSet)
		sprite.PlayAnimation(spriteSequence, spriteState.Value())
		spriteState.Add(Enumeration.State.crouch, priority)
	End Sub


	Public Sub GoDown()
		GoDown(Enumeration.PriorityState.Normal)
	End Sub
	Public Sub GoDown(offSet As Vector2)
		GoDown(Enumeration.PriorityState.Normal, offSet)
	End Sub
	Public Sub GoDown(priority As Enumeration.PriorityState)
		GoDown(Enumeration.PriorityState.Normal, Vector2.Zero)
	End Sub
	Public Sub GoDown(priority As Enumeration.PriorityState, offSet As Vector2)
		If isClimbableDown() = True Then
			ClimbDown()
			Return
		End If
		spriteState.Add(Enumeration.State.godown, priority, Nothing, Enumeration.SequenceReverse.Normal, offSet)
		sprite.PlayAnimation(spriteSequence, spriteState.Value())
		spriteState.Add(Enumeration.State.crouch, priority)
	End Sub

	Public Sub Stand()
		Stand(Enumeration.PriorityState.Normal, Nothing)
	End Sub

	Public Sub Stand(stoppable As Boolean)
		Stand(Enumeration.PriorityState.Normal, stoppable)
	End Sub

	Public Sub Stand(priority As Enumeration.PriorityState)
		Stand(priority, Nothing)
	End Sub

	Public Sub Stand(priority As Enumeration.PriorityState, stoppable As System.Nullable(Of Boolean))
		If priority = Enumeration.PriorityState.Normal And sprite.IsStoppable = stoppable Then
			Return
		End If

		Select Case spriteState.Previous().state
			Case Enumeration.State.freefall
				Crouch(Enumeration.PriorityState.Force)
				Return

			Case Enumeration.State.crouch
				StandUp()
				Return
			Case Enumeration.State.startrun
				RunStop()
				Return

			Case Enumeration.State.stand
				Return
			Case Else

				Exit Select
		End Select


		spriteState.Add(Enumeration.State.stand, priority)
		sprite.PlayAnimation(spriteSequence, spriteState.Value())
		spriteState.Add(Enumeration.State.stand, Enumeration.PriorityState.Normal)

	End Sub


	Public Sub StandJump()
		StandJump(Enumeration.PriorityState.Normal)
	End Sub
	Public Sub StandJump(priority As Enumeration.PriorityState)
		spriteState.Add(Enumeration.State.standjump, priority)
		sprite.PlayAnimation(spriteSequence, spriteState.Value())
	End Sub

	Public Sub StepFall()
		StepFall(Enumeration.PriorityState.Normal)
	End Sub
	Public Sub StepFall(priority As Enumeration.PriorityState)
		Dim offSet As Vector2 = Vector2.Zero
		StepFall(priority, offSet)
	End Sub
	Public Sub StepFall(priority As Enumeration.PriorityState, offSet As Vector2)
		PositionFall = Position.Value
		spriteState.Add(Enumeration.State.stepfall, priority, offSet)
		sprite.PlayAnimation(spriteSequence, spriteState.Value())
		spriteState.Add(Enumeration.State.freefall, priority)
	End Sub

	Public Sub HighJump()
		Select Case isClimbable()
			Case Enumeration.State.jumphangMed
				JumpHangMed()
				Return
			Case Enumeration.State.jumphangLong
				JumpHangLong()
				Return
		End Select

		spriteState.Add(Enumeration.State.highjump)
		sprite.PlayAnimation(spriteSequence, spriteState.Value())

		'isPressable();
	End Sub

	Public Sub ClimbFail()
		spriteState.Add(Enumeration.State.climbfail)
		sprite.PlayAnimation(spriteSequence, spriteState.Value())
	End Sub

	Public Sub HangDrop()
		spriteState.Add(Enumeration.State.hangdrop)
		sprite.PlayAnimation(spriteSequence, spriteState.Value())
	End Sub



	Public Sub RunTurn()
		If flip <> SpriteEffects.FlipHorizontally Then
			flip = SpriteEffects.None
		Else
			flip = SpriteEffects.FlipHorizontally
		End If

		spriteState.Add(Enumeration.State.runturn)
		sprite.PlayAnimation(spriteSequence, spriteState.Value())
	End Sub


	Public Sub Hang()
		'chek what kind of hang or hangstraight
		If spriteState.Value().state = Enumeration.State.hang Or spriteState.Value().state = Enumeration.State.hangstraight Then
			Return
		End If

		If IsFrontOfBlock(True) = True Then
			spriteState.Add(Enumeration.State.hangstraight)
		Else
			spriteState.Add(Enumeration.State.hang)
		End If

		sprite.PlayAnimation(spriteSequence, spriteState.Value())
	End Sub

	Public Sub Bump()
		Bump(Enumeration.PriorityState.Normal, Enumeration.SequenceReverse.Normal)
	End Sub

	Public Sub Bump(priority As Enumeration.PriorityState)
		Bump(priority, Enumeration.SequenceReverse.Normal)
	End Sub

	Public Sub Bump(priority As Enumeration.PriorityState, reverse As Enumeration.SequenceReverse)
		spriteState.Add(Enumeration.State.bump, priority, False, reverse)
		sprite.PlayAnimation(spriteSequence, spriteState.Value())
		spriteState.Add(Enumeration.State.stand, Enumeration.PriorityState.Normal)
	End Sub


	Public Sub RJumpFall()
		RJumpFall(Enumeration.PriorityState.Normal, Enumeration.SequenceReverse.Normal)
	End Sub

	Public Sub RJumpFall(priority As Enumeration.PriorityState)
		RJumpFall(priority, Enumeration.SequenceReverse.Normal)
	End Sub

	Public Sub RJumpFall(priority As Enumeration.PriorityState, reverse As Enumeration.SequenceReverse)
		spriteState.Add(Enumeration.State.rjumpfall, priority, False, reverse)
		sprite.PlayAnimation(spriteSequence, spriteState.Value())
	End Sub



	Public Sub RunJump()
		spriteState.Add(Enumeration.State.runjump)
		sprite.PlayAnimation(spriteSequence, spriteState.Value())
	End Sub

	Public Sub PickupSword()
		spriteState.Add(Enumeration.State.pickupsword)
		sprite.PlayAnimation(spriteSequence, spriteState.Value())
		Sword = True
		DirectCast(Maze.dContentRes(PrinceOfPersiaGame.CONFIG_SOUNDS & "presentation".ToUpper()), SoundEffect).Play()
	End Sub

	Public Sub DrinkPotion()
		spriteState.Add(Enumeration.State.drinkpotion)
		sprite.PlayAnimation(spriteSequence, spriteState.Value())
		Me.Energy = Me.LivePoints
	End Sub

	Public Sub Advance()
		spriteState.Add(Enumeration.State.advance, Enumeration.PriorityState.Normal)
		sprite.PlayAnimation(spriteSequence, spriteState.Value())

	End Sub


	Public Overloads Sub Strike()
		Strike(Enumeration.PriorityState.Normal)
	End Sub
	Public Overloads Sub Strike(priority As Enumeration.PriorityState)
		spriteState.Add(Enumeration.State.strike, priority)
		sprite.PlayAnimation(spriteSequence, spriteState.Value())
	End Sub


	Public Overloads Sub Retreat()
		spriteState.Add(Enumeration.State.retreat, Enumeration.PriorityState.Normal)
		sprite.PlayAnimation(spriteSequence, spriteState.Value())

	End Sub

	Public Sub Question()
		If spriteState.Value().state = Enumeration.State.hang Or spriteState.Value().state = Enumeration.State.hangstraight Then
			Hang()
		End If
	End Sub


End Class
